﻿using Architecture.Repository.Interface;
using Architeture.Domain;
using Architeture.Domain.DTO;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service
{
    public class EnterpriseService : IEnterpriseService
    {
        protected readonly IEnterpriseRepository enterpriseRepository;
        protected readonly IProvincelRepository provincelRepository;
        protected readonly ICityInfoRepository cityInfoRepository;
        protected readonly ICountyRepository countyRepository;

        public EnterpriseService(IEnterpriseRepository enterpriseRepository, IProvincelRepository provincelRepository, ICityInfoRepository cityInfoRepository, ICountyRepository countyRepository)
        {
            this.enterpriseRepository = enterpriseRepository;
            this.provincelRepository = provincelRepository;
            this.cityInfoRepository = cityInfoRepository;
            this.countyRepository = countyRepository;
        }

        public Task<int> EnterpriseAdd(EnterpriseInfo a)
        {
            return enterpriseRepository.Update(a);
        }

        public async Task<int> EntRlaughterAdd(EnterpriseInfo a)
        {
            return await enterpriseRepository.Update(a);
        }

        public async Task<EnterpriseInfo> GetEnt(int id)
        {
            var list = (await enterpriseRepository.GetAllAsync()).FirstOrDefault(x => x.GoodsInfoid == id);
            return list;
        }

        public async Task<List<EnterDTO>> GetEnterDTO(int id)
        {
            var pro = await provincelRepository.GetAllAsync();
            var cit = await cityInfoRepository.GetAllAsync();
            var cou = await countyRepository.GetAllAsync();
            var list = from a in await enterpriseRepository.GetAllAsync()
                       join b in pro on a.Address equals b.ProvinceInfoId
                       join c in cit on a.city equals c.CityInfoId
                       join d in cou on a.county equals d.CountyInfoId
                       where a.GoodsInfoid == id
                       select new EnterDTO
                       {
                           EnterpriseInfoid = a.EnterpriseInfoid,
                           GoodsInfoid = a.GoodsInfoid,
                           Breedingfarm = a.Breedingfarm,
                           charge = a.charge,
                           Numberofstorage = a.Numberofstorage,
                           Nameofcompany = a.Nameofcompany,
                           Address = a.Address,
                           city = a.city,
                           county = a.county,
                           HaveTime = a.HaveTime,
                           Introduction = a.Introduction,
                           Processingcompany = a.Processingcompany,
                           Productionlicensenumber = a.Productionlicensenumber,
                           ProvinceInfo = b.ProvinceName,
                           CityInfo = c.CityName,
                           CountyInfo = d.CountyInfoName
                       };
            return list.ToList();
        }

        public async Task<EnterpriseInfo> SetEntinfo(int id)
        {
            return (await enterpriseRepository.GetAllAsync()).FirstOrDefault(x => x.GoodsInfoid == id);
        }
    }
}
